window.onload = function () {
    getEpidemicData();
    getBulletin();

    // 填充用户真是姓名
    fillUserRealName();
}

var layer, table, carousel;
layui.use(['carousel', 'layer', 'laydate', 'table'], function () {
    layer = layui.layer;
    table = layui.table;
    carousel = layui.carousel;
});

/**
 * 获取疫情数据
 */
function getEpidemicData() {
    $.ajax({
        type: "POST",
        url: "/common/getEpidemicData",
        contentType: "application/json",
        sync: true,
        data: {},
        dataType: "json",
        success: function (result) {
            var data = result.data;
            if (result.code == 200) {
                var currentConfirmedCount = data.currentConfirmedCount;
                var currentConfirmedIncr = data.currentConfirmedIncr;
                var confirmedCount = data.confirmedCount;
                var seriousCount = data.seriousCount;
                var curedCount = data.curedCount;
                var suspectedIncr = data.suspectedIncr;
                var seriousIncr = data.seriousIncr;
                var confirmedIncr = data.confirmedIncr;
                var curedIncr = data.curedIncr;
                var deadCount = data.deadCount;
                var deadIncr = data.deadIncr;
                var suspectedCount = data.suspectedCount;

                // 现存确诊
                if (currentConfirmedIncr != 0) {
                    $('#currentConfirmedIncrStr').empty().append("较昨日");
                    $('#currentConfirmedIncr').empty().append((currentConfirmedIncr > 0 ? '+' : '-') + currentConfirmedIncr);
                } else {
                    $('#currentConfirmedIncrStr').empty().append("较昨日无变化");
                    $('#currentConfirmedIncr').empty();
                }
                $('#currentConfirmedCount').empty().append(currentConfirmedCount);

                // 境外输入
                if (suspectedIncr != 0) {
                    $('#suspectedIncrStr').empty().append("较昨日");
                    $('#suspectedIncr').empty().append((suspectedIncr > 0 ? '+' : '-') + suspectedIncr);
                } else {
                    $('#suspectedIncrStr').empty().append("较昨日无变化");
                    $('#suspectedIncr').empty();
                }
                $('#suspectedCount').empty().append(suspectedCount);

                // 现存无症状
                if (seriousIncr != 0) {
                    $('#seriousIncrStr').empty().append("较昨日");
                    $('#seriousIncr').empty().append((seriousIncr > 0 ? '+' : '-') + seriousIncr);
                } else {
                    $('#seriousIncrStr').empty().append("较昨日无变化");
                    $('#seriousIncr').empty();
                }
                $('#seriousCount').empty().append(seriousCount);

                // 累计确诊
                if (confirmedIncr != 0) {
                    $('#confirmedIncrStr').empty().append("较昨日");
                    $('#confirmedIncr').empty().append((confirmedIncr > 0 ? '+' : '-') + confirmedIncr);
                } else {
                    $('#confirmedIncrStr').empty().append("较昨日无变化");
                    $('#confirmedIncr').empty();
                }
                $('#confirmedCount').empty().append(confirmedCount);

                // 累计死亡
                if (deadIncr != 0) {
                    $('#deadIncrStr').empty().append("较昨日");
                    $('#deadIncr').empty().append((deadIncr > 0 ? '+' : '-') + deadIncr);
                } else {
                    $('#deadIncrStr').empty().append("较昨日无变化");
                    $('#deadIncr').empty();
                }
                $('#deadCount').empty().append(deadCount);

                if (curedIncr != 0) {
                    $('#curedIncrStr').empty().append("较昨日");
                    $('#curedIncr').empty().append((curedIncr > 0 ? '+' : '-') + curedIncr);
                } else {
                    $('#curedIncrStr').empty().append("较昨日无变化");
                    $('#curedIncr').empty();
                }
                $('#curedCount').empty().append(curedCount);
            }
        }
    });
}

/**
 * 获取最新公告
 */
function getBulletin() {
    $.ajax({
        type: "POST",
        url: "/bulletin/getHotBulletins",
        //contentType: "application/json",
        sync: true,
        data: {},
        dataType: "json",
        success: function (result) {
            console.log(result)
            if (result.code == 200) {
                var stu = result.data;
                $("#stuName").val(stu.stuName);

                $("#bulletinList").empty();
                $.each(result.data, function (i, o) {
                    var opt = '<div onclick="viewBulletin(' + o.bulletinId + ')" style="cursor:pointer; background-size: auto 100%; background-repeat: no-repeat; background-image: url(' + o.bulletinImg + '); height: 100%; text-align: center">' +
                        '<div style="position: relative; top: 45%; font-size:30px; font-weight:bold; color:white;">' + o.bulletinTitle + '</div>';
                    $("#bulletinList").append(opt);
                });
                // 图片轮播
                carousel.render({
                    elem: '#bulletin',
                    width: '90%',
                    height: '90%',
                    interval: 1800,
                    anim: 'default', // 上下切换
                    // arrow: 'hover'// 箭头悬停显示
                });
                //$("#bulletinList").reload();//动态加载
            } else {
                console.log("该学生不存在！")
            }
        }
    });
}

/**
 * 查看公告
 */
function viewBulletin(bulletinId) {
    var url = '/viewBulletin?bulletinId=' + bulletinId;
    window.location.href = url;
}

// 填充用户真实姓名
function fillUserRealName() {
    var userRealName = getCookie('userRealName');
    debugger
    if (!$.isEmptyObject(userRealName)) {
        $("#userRealName").empty().append(userRealName)
    }
}

// 获取指定cookie的值
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i].trim();
        if (c.indexOf(name) == 0) {
            return decodeURI(c.substring(name.length, c.length));
        }
    }
    return "";
}